<div class="container-fluid">
<div class="row">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-6 col-lg-6">

<?php

require_once  __DIR__ . '/includes/authenticate.php';

require_once __DIR__ . '/includes/BasketbalHandlerFunctions.php';

$basketbalhandlerfunctions = new BasketbalHandlerFunctions();

if (isset($_POST['season']) && isset($_POST['gamenumber']) && isset($_POST['playerID']) && isset($_POST['quarter']) && !isset($_POST['DRB'])) {

  include_once './db_bb_connect.php';
	
	$season = $_POST['season'];
	$gamenumber = $_POST['gamenumber'];
	$playerID = $_POST['playerID'];
	$quarter = $_POST['quarter'];

	$sql = "SELECT * 
	FROM stats_game_quarter 
	WHERE season = '$season' 
	AND gamenumber = '$gamenumber'
	AND playerID = '$playerID'
	AND quarter = '$quarter'";

	$result = $db->query($sql);

	$row = $result->fetch_assoc();

	$DRB = $row['DRB']; 
  	$ORB = $row['ORB']; 
  	$TOV = $row['TOV']; 
  	$ST = $row['ST']; 
    $AST = $row['AST']; 
    $BS = $row['BS'];
  	$FTscored = $row['FTscored']; 
  	$FTtotal = $row['FTtotal']; 
  	$FGIscored = $row['FGIscored']; 
  	$FGItotal = $row['FGItotal'];
  	$PF = $row['PF'];
  	$FGOscored = $row['FGOscored']; 
  	$FGOtotal = $row['FGOtotal'];
  	$THPscored = $row['THPscored']; 
  	$THPtotal = $row['THPtotal'];

    $minutesplayed = floor((int)$row['MP'] / 60);
	$seconds = (int)$row['MP'] % 60;

	if ( $seconds >= 0 && $seconds < 10) {
		$secondsplayed = "0$seconds";
	} else {
		$secondsplayed = $seconds;
	}

    $MP = "$minutesplayed:$secondsplayed"; 

  $sql2 = "SELECT firstname, lastname 
  FROM users 
  WHERE user_key = '$playerID'";

  $result2 = $db->query($sql2);
  $row2 = $result2->fetch_assoc();
  $firstname = $row2['firstname']; 
  $lastname = $row2['lastname'];

  $sql3 = "SELECT opponent, location 
  FROM games 
  WHERE season = '$season'
  AND gamenumber = '$gamenumber'";

  $result3 = $db->query($sql3);
  $row3 = $result3->fetch_assoc();
  $opponent = $row3['opponent']; 
  $location = $row3['location'];




?>



<center><h4>Editting stats of <?= $firstname; ?> <?= $lastname; ?> <br> <?= $season; ?> Game <?= $gamenumber; ?> Q<?= $quarter; ?>  - <?= $opponent; ?> <?= ucfirst($location) ?></h4> </center>
<br>

<form>
 <div class="form-group">

 	<input type="hidden" name="season" value="<?= $season; ?>"/>

  <input type="hidden" name="playerID" value="<?= $playerID; ?>"/>
  
  <input type="hidden" name="gamenumber" value="<?= $gamenumber; ?>"/>

  <input type="hidden" name="quarter" value="<?= $quarter; ?>"/>


	<div class="input-group">
        <input class="form-control" id="ex1" type="number" placeholder="DRB" name="DRB" value="<?= $DRB; ?>">
       <span class="input-group-addon">DRB</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex2" type="number" placeholder="ORB" name="ORB" value="<?= $ORB; ?>">
        <span class="input-group-addon">ORB</span>
      </div>
      <br/>
     <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="TOV" name="TOV" value="<?= $TOV; ?>">
        <span class="input-group-addon">TO</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="ST" name="ST" value="<?= $ST; ?>">
        <span class="input-group-addon">ST</span>
      </div>
      <br/>

	<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="AST" name="AST" value="<?= $AST; ?>">
        <span class="input-group-addon">AST</span>
      </div>
      <br/>

      	<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="BS" name="BS" value="<?= $BS; ?>">
        <span class="input-group-addon">BS</span>
      </div>
      <br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="<?= $placeholderscored; ?>" name="FGIscored" value="<?= $FGIscored; ?>">
        <span class="input-group-addon">FGI scored
          
        </span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="<?= $placeholdertotal; ?>" name="FGItotal" value="<?= $FGItotal; ?>">
        <span class="input-group-addon">FGI total
        </span>
      </div>
      <br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside scored" name="FGOscored" value="<?= $FGOscored; ?>">
        <span class="input-group-addon">FGO scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside total" name="FGOtotal" value="<?= $FGOtotal; ?>">
        <span class="input-group-addon">FGO total</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3p scored" name="THPscored" value="<?= $THPscored; ?>">
        <span class="input-group-addon">3p scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3p total" name="THPtotal" value="<?= $THPtotal; ?>">
        <span class="input-group-addon">3p total</span>
</div>
<br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FT scored" name="FTscored" value="<?= $FTscored; ?>">
        <span class="input-group-addon">FT scored</span>
      </div>
      <br/>
      <div class="input-group">
       <input class="form-control" id="ex3" type="number" placeholder="FT total" name="FTtotal" value="<?= $FTtotal; ?>">
         <span class="input-group-addon">FT total</span>
      </div>
      <br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="PF" name="PF" value="<?= $PF; ?>">
        <span class="input-group-addon">PF</span>
       </div>
      <br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="text" placeholder="Minutes Played 00:00" name="MP" value="<?= $MP; ?>">
        <span class="input-group-addon">MP</span>
</div>
<br/>

      <button class="btn btn-default submit" type="submit">Update Entry</button>

</div>

</form>

<?php } elseif (isset($_POST['DRB'])) {

	  foreach ($_POST as $key => $value) {
      if ($value == '') {
        $value = ":null";
        $$key = $value;
      } else {
        	$$key = $value;
      }
    }

    $MPsplit = explode(":", $MP);
    $MP = 60 * (int)$MPsplit[0] + (int)$MPsplit[1];

	$basketbalhandlerfunctions->UpdatePlayerQuarterStats($season, $gamenumber, $quarter, $playerID, $DRB, $ORB, $ST, $TOV, $AST, $BS, $FGIscored, $FGItotal, $FGOscored, $FGOtotal, $THPscored, $THPtotal, $FTscored, $FTtotal, $PF, $MP);

}

?>
</div>

<div class="col-md-3 col-lg-3"></div>
</div>
</div>

<script type="text/javascript">

$(".submit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
      type: "POST",
        url: "./editplayerstats_quarter.php",
        data: postData,
        success: function(data) {  
          $("#entirestatspage").html(data);
          
        },
        error: function (xhr, status, error) {
          $("#entirestatspage").html(xhr.responseText);
        }
      }); 
    e.preventDefault();
});

</script>